#################################################
# Welcome to the main Aero gameplay file, game.rb. This is where the magic happens.   #
# Okay, not really magic, just ugly code that gets the job done. I will explain where    #
# I can what is going on, mainly for myself so that I can understand the logic behind   #
# this game.                                                                                                       #
#                                                                                                                      #
# Owner: Matt Wanninger                                                                                   #
#################################################


#------module ZOrder--------------------------------------------------------------------
#   This module contains the values for various 'z' levels used for Gosu images.
#    A 'z' value of a higher order (number) will be drawn on top of a z value of
#    a lower number; ie, Z::Order::Player will be drawn over Z::Order::Background.
#-------------------------------------------------------------------------------------------

module ZOrder
  Background, Parallax1, Parallax2, Parallax3, Parallax4, Parallax5, Objects, Player, Projectile, Explosion, UI, Text = *0..10
end



#------class Game--------------------------------------------------------------------------
#    This class contains the initialize, update, and draw methods used for actual
#    gameplay. As in other classes:
#      * 'initialize' method loads all the images/ music/global variables used in 
#         the various methods in the class, as well as setting the initial game state. 
#      * 'update' is where the calculations are done and player inputs are read
#      * 'draw' of course simply draws the state of the game on screen based on
#         the variables
#    The rest of the methods are helpers and may eventually be moved to a
#    separate file.
#    Inputs:
#      * level - level # to launch
#      * letters - font initialized in frontend.rb
#      * glbackground - glbackground object (3d background effect) initialized in
#        frontend.rb
#---------------------------------------------------------------------------------------------

class Game
  attr_accessor :scroll_x, :scroll_speed, :screen_width, :screen_height
  
  #-------method initialize------------------------------------------------------------------
  #    As described in the class description, 'initialize' loads all the global variables,
  #    images, and music used
  #-------------------------------------------------------------------------------------------
  
  def initialize(level,  letters)
    @screen_width = 640
    @screen_height = 480
    @guns = 0
    @level1space = Images['media/galaxy.png', false]
    @level2space = Images['media/space01.png',false]
    @sidebar = Images['media/sidebar.png', false]
    @debris = Gosu::Image::load_tiles($screen, 'media/debris.png', 5, 5, true)
    @planet = Images['media/planet01.png', false]
    @planet2 = Images['media/planet02.png', false]
    @planet3 = Images['media/planet03.png', false]
    @background = Array.new
    @level1_objects = Array.new
    @level1_parallax5 = Array.new
    @level1_parallax4 = Array.new
    @level1_parallax3 = Array.new
    @level1_parallax2 = Array.new
    @level1_parallax = Array.new
    
    @player_anim = Gosu::Image::load_tiles($screen,'media/jet.png', 40, 46, true)
    @player = Player.new(@player_anim, @screen_width / 2.0 , @screen_height - 75, 3, 25)
    @thrusters = 'media/thrusters.png'
    @thrustersred = 'media/explosion.png'


    @pirate1 = Gosu::Image::load_tiles($screen, "media/pirate.bmp", 40, 45, false)
    @enemies = Array.new
    
    @projectiles_anim = Gosu::Image.new($screen, "media/blueshot.bmp", false)
    @projectiles = Array.new
    @enemy_projectile = Gosu::Image.new($screen, "media/basicshot.bmp", false)
    @particles = Array.new
    @projectilecolor1 = Gosu::Color.new(255, 255 ,255, 255)
    @projectilecolor2 = Gosu::Color.new(255,  175, 175, 255)

    @hit_anim = Gosu::Image::load_tiles($screen, "media/shotimpact.bmp", 10, 10, false)
    @hits = Array.new
    
    
    

    @level1 = false
    @level2 = false
    @level3 = false
    if level == 1
      @level3 = true
    elsif level == 2
      @level2 = true
    elsif level ==3
      @level1 = true
    end
    
    @pause = false
    @firing_state = false
    @fail = false
    @playerupdate = false
    

    @fire = Sounds["media/laser.wav"]
    @enemy_fire = Sounds["media/sound7.wav"]
    @explode = Sounds["media/explosion-02.wav"]
    @hit_sound = Sounds["media/Explosion.wav"]
    @takeoff_sound = Sounds["media/takeoff.wav"]
    @intro = Sounds['media/voyage.ogg']
    @intro2 = Sounds['media/darkmatter.ogg']
    @intro3 = Sounds['media/boss.ogg']
    if @level1
      SDL::Mixer.playChannel(0, @intro, -1)
    elsif @level2
      SDL::Mixer.playChannel(0, @intro2, -1)
    elsif @level3
      SDL::Mixer.playChannel(0, @intro3, -1)
    end
    SDL::Mixer.setVolume(-1, 20)
    SDL::Mixer.setVolume(0, 128)
    

    @font = Gosu::Font.new($screen, Gosu::default_font_name, 24)
    
     if @level1
      backgroundInit1
    elsif @level2
      backgroundInit2
    elsif @level3
      backgroundInit3
    end
    
    @a = 0
    @b = 1
    @field1 = false
    @wave1 = false
    @wave2 = false
    @wave3 = false
    @field2 = false
    @wave4 = false
    @wave5 = false
    @wave = Wave.new(@enemies)
    @letters = letters
    @level = level
  end
  
  
  #-------method update--------------------------------------------------------------------
  #    As described in the class description, 'update' performs the calculations and 
  #    updates the state of the game based on the previous state and player inputs
  #-------------------------------------------------------------------------------------------
  
  def update
    #Ensure music volume is max, as music is always on channel 0 and tends to get covered up by
    #other sounds
    SDL::Mixer.setVolume(0, 128)

    if(!@pause)
      #if @level1
      if @level1
        generateBackground1
        enemyUpdate1
        inputUpdate
        collisionUpdate
        particleUpdate
      elsif @level2
        generateBackground2
        enemyUpdate2
        inputUpdate
        collisionUpdate
        particleUpdate
      elsif @level3
        generateBackground3
        enemyUpdate3
        inputUpdate
        collisionUpdate
        particleUpdate
      else
      end 
    end 
    
    if @pause
      pauseScreen
      SDL::Mixer.pause(0)
    elsif SDL::Mixer.pause?(0)
      SDL::Mixer.resume(0)
    end
    
    if @fail
      if !@pause
       inputUpdate
      end
    end
  end
  
  
  #-------method draw--------_------------------------------------------------------------
  #    As described in the class description, 'draw' displays the visual state of the 
  #    game on screen
  #-------------------------------------------------------------------------------------------
  
  def draw
    
    if @level1 or @level2 or @level3
      @particles.each{|particle| particle.draw}
      @player.draw
      @background.each{ |background| background.draw}
      @level1_parallax.each{ |background| background.draw} 
      @level1_parallax2.each{ |background| background.draw}
      @level1_parallax3.each{ |background| background.draw}
      @level1_parallax4.each{ |background| background.draw}
      @level1_parallax5.each{ |background| background.draw}
      @level1_objects.each{ |background| background.draw}
      @enemies.each{ |enemy| enemy.draw }
      @projectiles.each{ |projectile| projectile.draw }
      @hits.each{ |hit| hit.draw}
      drawUI
    end 
    
    if @pause then
        drawPause
      end
    
    if @fail
      @font.draw_rel("FAIL", 320, 150, ZOrder::UI, 0.5, 0.5, 1.0, 1.0, 0xffff0000)
      @font.draw_rel("(Press Esc)", 320, 200, ZOrder::UI, 0.5, 0.5, 1.0, 1.0, 0xffff0000)
    end
  end 
  
  
#*********************************************************************************
#******************************Helper Methods************************************
#*********************************************************************************
  
  def drawPause
    @font.draw_rel("Quit?", @screen_width / 2.0, @screen_height/2.0, ZOrder::UI, 0.5, 0.5, 1.0, 1.0, 0xffcccccc)
    @font.draw_rel("Enter - Yes.", @screen_width / 2.0, @screen_height/2.0 + 20, ZOrder::UI, 0.5, 0.5, 1.0, 1.0, 0xffcccccc)
    @font.draw_rel("Esc - Return to Game", @screen_width / 2.0, @screen_height/2.0 + 40, ZOrder::UI, 0.5, 0.5, 1.0, 1.0, 0xffcccccc)
  end
  
  #Initialize first background images
  def backgroundInit1
    @background.push(Background.new(@level1space, @screen_width / 2.0, 0, 0.05, 1.0, 1.0, ZOrder::Background))
    @background.push(Background.new(@sidebar, 60, 240, 0.0, 1.0, 1.0, ZOrder::UI))
    @background.push(Background.new(@sidebar, 580, 240, 0.0, 1.0, 1.0, ZOrder::UI))
    5.times do
      @level1_parallax5.push(Background.new(@debris[rand(2)], rand(400)+120, rand(480), 1.75, 1.0, 1.0, ZOrder::Parallax5))
      @level1_parallax4.push(Background.new(@debris[0+10], rand(400)+120, rand(480), 1.25, 1.0, 1.0, ZOrder::Parallax4))
      @level1_parallax3.push(Background.new(@debris[0+20], rand(400)+120, rand(480), 0.5, 1.0, 1.0, ZOrder::Parallax3))
    end
   end
   
  def backgroundInit2
    @background.push(Background.new(@level2space, @screen_width / 2.0, 0, 0.05, 1.0, 1.0, ZOrder::Background))
    @background.push(Background.new(@sidebar, 60, 240, 0.0, 1.0, 1.0, ZOrder::UI))
    @background.push(Background.new(@sidebar, 580, 240, 0.0, 1.0, 1.0, ZOrder::UI))
    5.times do
      @level1_parallax5.push(Background.new(@debris[rand(2)], rand(400)+120, rand(480), 1.75, 1.0, 1.0, ZOrder::Parallax5))
      @level1_parallax4.push(Background.new(@debris[0+10], rand(400)+120, rand(480), 1.25, 1.0, 1.0, ZOrder::Parallax4))
      @level1_parallax3.push(Background.new(@debris[0+20], rand(400)+120, rand(480), 0.5, 1.0, 1.0, ZOrder::Parallax3))
    end
   end
    
    def backgroundInit3
    @background.push(Background.new(@sidebar, 60, 240, 0.0, 1.0, 1.0, ZOrder::UI))
    @background.push(Background.new(@sidebar, 580, 240, 0.0, 1.0, 1.0, ZOrder::UI))
  end
  
  #Detects whether a collision has occurred between a player projectile and an enemy, or an enemy/enemy projectile with the player
  def collisionUpdate
    @projectiles.reject! { |projectile| projectile.update}
    @hits.reject! { |hit| !hit.update}
    @player.collision(@enemies, @projectiles, @explode, @particles, @hit_sound)
    @projectiles.reject! {|projectile| projectile.collision(@enemies, @player, @hits, @hit_anim, @explode, @particles, @hit_sound)}
     if @player.update  #Basically, if player dies, level 1 is over and the "fail" screen is loaded.
        if @level1
            @level1 = false
        elsif @level2
            @level2 = false
        elsif @level3
            @level3 = false
        end
        @fail = true
    end
  end
  
  #Draw user interface on side of screen
  def drawUI
    @caption = "1p"
    @length = @caption.length
    @i = 0
    @x = 10
    @length.times{
      @string = @caption[@i, 1]
      @img = @letters[@string]
      @img.draw_rot(@x, 20, ZOrder::UI, 0, 0.5,0.5, 0.75,0.75)
      @i += 1
      @x += 24
    }
    
    @caption = "lives"
    @length = @caption.length
    @i = 0
    @x = 25
    @length.times{
      @string = @caption[@i, 1]
      @img = @letters[@string]
      @img.draw_rot(@x, 400, ZOrder::UI, 0, 0.5,0.5, 0.5,0.5)
      @i += 1
      @x += 16
    }
    @lives = @player.lives
    @tempimage = @player_anim[3]
    @tempx = 11
    @lives.times{
        @tempimage.draw_rot(@tempx, 430, ZOrder::UI, 0, 0.5, 0.5, 0.5, 0.5)
        @tempx += 24
    }
    
    @caption = "shield"
    @length = @caption.length
    @i = 0
    @x = 18
    @length.times{
      @string = @caption[@i, 1]
      @img = @letters[@string]
      @img.draw_rot(@x, 330, ZOrder::UI, 0, 0.5,0.5, 0.5,0.5)
      @i += 1
      @x += 16
    }
    
    @caption = "level #{@level}"
    @length = @caption.length
    @i = 0
    @x = 534
    @length.times{
      @string = @caption[@i, 1]
      @img = @letters[@string]
      @img.draw_rot(@x, 170, ZOrder::UI, 0, 0.5,0.5, 0.5,0.5)
      @i += 1
      @x += 16
    }
    if @level ==1
      @caption = "barren"
      @length = @caption.length
      @i = 0
      @x = 545
      @length.times{
        @string = @caption[@i, 1]
        @img = @letters[@string]
        @img.draw_rot(@x, 200, ZOrder::UI, 0, 0.5,0.5, 0.4,0.4)
        @i += 1
        @x += 12.8
      }
      @caption = "world"
      @length = @caption.length
      @i = 0
      @x = 550
      @length.times{
        @string = @caption[@i, 1]
        @img = @letters[@string]
        @img.draw_rot(@x, 220, ZOrder::UI, 0, 0.5,0.5, 0.4,0.4)
        @i += 1
        @x += 12.8
      }
    end
    @caption = "#{@player.score}"
    @length = @caption.length
    @i = 0
    @x = 10
    @length.times{
      @string = @caption[@i, 1]
      @img = @letters[@string]
      @img.draw_rot(@x, 50, ZOrder::UI, 0, 0.5,0.5, 0.5,0.5)
      @i += 1
      @x += 16
    }
    
    @hp = @player.hp
    @shieldy = 350
    20.times{
      $screen.draw_line(4.0, @shieldy, 0xff66ffff, 4+110.0*(@hp/10.0), @shieldy, 0xff66ffff, ZOrder::UI, mode=:default)
      @shieldy+=1
    }
    $screen.draw_line(119, 0, 0xff66ffff, 119, 480, 0xff66ffff, ZOrder::UI, mode=:default)
    $screen.draw_line(521, 0, 0xff66ffff, 119+401, 480, 0xff66ffff, ZOrder::UI, mode=:default)
  end
  
  #Update enemies on screen
  def enemyUpdate1
      if @background[0].y > 3 and !@field1
        @wave.generateAsteroidField
        @field1 = true
      end
      if @background[0].y > 30 and !@wave1
        @wave.generateSpreadWave(6, 250)
        @wave1 = true
      end
       if @background[0].y > 35 and !@wave2
        @wave.generateLineWave(7, 460)
        @wave2 = true
      end
      if @background[0].y > 38 and !@wave3
        @wave.generateLineWave(7, 180)
        @wave3 = true
      end
       if @background[0].y > 45 and !@field2
        @wave.generateAsteroidField
        @field2 = true
      end
      if @background[0].y > 72 and !@wave4
        @wave.generateSpreadWave(5, 150)
        @wave4 = true
      end
      if @background[0].y > 75 and !@wave5
        @wave.generateSpreadWave(5, 250)
        @wave5 = true
      end
      @enemies.reject! { |enemy|
        if enemy.type == 0
          1.times {@particles.push( Particle.new(ZOrder::Player, enemy.x - 0.5 , enemy.y - enemy.radius, @thrustersred, (rand(100) - 50.0) / 1000.0, -0.75, rand(360), 0,  0.3, 0.1, 70))}
        end  
        if rand(400) > 395 and enemy.type==0
          @projectiles.push(Projectile.new(@enemy_projectile, enemy.x, enemy.y + enemy.radius, 1, 0, 7, rand(360), false, @projectilecolor1))
          SDL::Mixer.playChannel(-1, @enemy_fire, 0)
        end
        !enemy.update}
    end
    
     #Update enemies on screen
  def enemyUpdate2
      if @background[0].y > 3 and !@field1
        @wave.generateAsteroidField
        @field1 = true
      end
      if @background[0].y > 30 and !@wave1
        @wave.generateSpreadWave(6, 250)
        @wave1 = true
      end
       if @background[0].y > 35 and !@wave2
        @wave.generateLineWave(7, 460)
        @wave2 = true
      end
      if @background[0].y > 38 and !@wave3
        @wave.generateLineWave(7, 180)
        @wave3 = true
      end
       if @background[0].y > 45 and !@field2
        @wave.generateAsteroidField
        @field2 = true
      end
      if @background[0].y > 72 and !@wave4
        @wave.generateSpreadWave(5, 150)
        @wave4 = true
      end
      if @background[0].y > 75 and !@wave5
        @wave.generateSpreadWave(5, 250)
        @wave5 = true
      end
      @enemies.reject! { |enemy|
        if enemy.type == 0
          1.times {@particles.push( Particle.new(ZOrder::Player, enemy.x - 0.5 , enemy.y - enemy.radius, @thrustersred, (rand(100) - 50.0) / 1000.0, -0.75, rand(360), 0,  0.3, 0.1, 70))}
        end  
        if rand(400) > 395 and enemy.type==0
          @projectiles.push(Projectile.new(@enemy_projectile, enemy.x, enemy.y + enemy.radius, 1, 0, 7, rand(360), false, @projectilecolor1))
          SDL::Mixer.playChannel(-1, @enemy_fire, 0)
        end
        !enemy.update}
    end
    
     #Update enemies on screen
  def enemyUpdate3
        
   end
  
  #Add new elements randomly to background scrolling and update all parallax levels
  def generateBackground1
      if rand(200) < 4 and @level1_parallax5.size < 25
        @level1_parallax5.push(Background.new(@debris[rand(2)], rand(400)+120, -5, 1.75, 1.0, 1.0, ZOrder::Parallax5))
      end
      if rand(250) < 4 and @level1_parallax4.size < 30
        @level1_parallax4.push(Background.new(@debris[0+10], rand(400)+120, -5, 1.25, 1.0, 1.0, ZOrder::Parallax4))
      end
      if rand(300) < 4 and @level1_parallax3.size < 35
        @level1_parallax3.push(Background.new(@debris[0+20], rand(400)+120, -5, 0.5, 1.0, 1.0, ZOrder::Parallax3))
      end
      @level1_parallax.reject! { |background| !background.update }
      @level1_parallax2.reject! { |background| !background.update }
      @level1_parallax3.reject! { |background| !background.update }
      @level1_parallax4.reject! { |background| !background.update }
      @level1_parallax5.reject! { |background| !background.update }
      @level1_objects.reject! { |background| !background.update }
      @background.reject!{|background| !background.update}
    end
    
    def generateBackground2
      if rand(200) < 4 and @level1_parallax5.size < 25
        @level1_parallax5.push(Background.new(@debris[rand(2)], rand(400)+120, -5, 1.75, 1.0, 1.0, ZOrder::Parallax5))
      end
      if rand(250) < 4 and @level1_parallax4.size < 30
        @level1_parallax4.push(Background.new(@debris[0+10], rand(400)+120, -5, 1.25, 1.0, 1.0, ZOrder::Parallax4))
      end
      if rand(300) < 4 and @level1_parallax3.size < 35
        @level1_parallax3.push(Background.new(@debris[0+20], rand(400)+120, -5, 0.5, 1.0, 1.0, ZOrder::Parallax3))
      end
      if rand(1000) < 4 and @level1_objects.size < 1
        @level1_objects.push(Background.new(@planet, rand(400)+120, -99, 0.25, 1.0, 1.0, ZOrder::Parallax1))
      end
      if rand(1000) < 4 and @level1_objects.size < 1
        @level1_objects.push(Background.new(@planet2, rand(400)+120, -99, 0.25, 1.0, 1.0, ZOrder::Parallax1))
      end
      if rand(1000) < 4 and @level1_objects.size < 1
        @level1_objects.push(Background.new(@planet3, rand(400)+120, -99, 0.25, 1.0, 1.0, ZOrder::Parallax1))
      end
      @level1_parallax.reject! { |background| !background.update }
      @level1_parallax2.reject! { |background| !background.update }
      @level1_parallax3.reject! { |background| !background.update }
      @level1_parallax4.reject! { |background| !background.update }
      @level1_parallax5.reject! { |background| !background.update }
      @level1_objects.reject! { |background| !background.update }
      @background.reject!{|background| !background.update}
    end
    
    def generateBackground3
      
      @level1_parallax.reject! { |background| !background.update }
      @level1_parallax2.reject! { |background| !background.update }
      @level1_parallax3.reject! { |background| !background.update }
      @level1_parallax4.reject! { |background| !background.update }
      @level1_parallax5.reject! { |background| !background.update }
      @level1_objects.reject! { |background| !background.update }
      @background.reject!{|background| !background.update}
   end
  
  #Update user inputs and act accordingly
  def inputUpdate
    if $screen.button_down? Gosu::Button::KbLeftControl and !@player.died then
      @a += 1
      if @firing_mode == false or @a % 7 == 0
        SDL::Mixer.setVolume(1, 10)        
        SDL::Mixer.playChannel(1, @fire, 0)
        if @guns==0
          @projectiles.push(Projectile.new(@projectiles_anim, @player.x-12, @player.y+7, 1, 0, -15, 180, true, @projectilecolor1))
          @projectiles.push(Projectile.new(@projectiles_anim, @player.x+12, @player.y+7, 1, 0, -15, 180, true, @projectilecolor1))
        elsif @guns==1
          @projectiles.push(Projectile.new(@projectiles_anim, @player.x-10, @player.y+7, 1, 0, -15, 180, true,@projectilecolor1))
          @projectiles.push(Projectile.new(@projectiles_anim, @player.x+10, @player.y+7, 1, 0, -15, 180, true,@projectilecolor1))
          @projectiles.push(Projectile.new(@projectiles_anim, @player.x-14, @player.y+7, 1, 0, -15, 180, true,@projectilecolor1))
          @projectiles.push(Projectile.new(@projectiles_anim, @player.x+14, @player.y+7, 1, 0, -15, 180, true,@projectilecolor1))
        elsif @guns==2
          @projectiles.push(Projectile.new(@projectiles_anim, @player.x-10, @player.y+7, 2, 0, -15, 180, true,@projectilecolor2))
          @projectiles.push(Projectile.new(@projectiles_anim, @player.x+10, @player.y+7, 2, 0, -15, 180, true,@projectilecolor2))
          @projectiles.push(Projectile.new(@projectiles_anim, @player.x-14, @player.y+7, 2, 0, -15, 180, true,@projectilecolor2))
          @projectiles.push(Projectile.new(@projectiles_anim, @player.x+14, @player.y+7, 2, 0, -15, 180, true,@projectilecolor2))
          @projectiles.push(Projectile.new(@projectiles_anim, @player.x-14, @player.y+7, 1, -6, -15, 170, true,@projectilecolor1))
          @projectiles.push(Projectile.new(@projectiles_anim, @player.x+14, @player.y+7, 1, 6, -15, 190, true,@projectilecolor1))
          @projectiles.push(Projectile.new(@projectiles_anim, @player.x-14, @player.y+7, 1, -4, -15, 173, true,@projectilecolor1))
          @projectiles.push(Projectile.new(@projectiles_anim, @player.x+14, @player.y+7, 1, 4, -15, 187, true,@projectilecolor1))
          @projectiles.push(Projectile.new(@projectiles_anim, @player.x-14, @player.y+7, 1, -2, -15, 175, true,@projectilecolor1))
          @projectiles.push(Projectile.new(@projectiles_anim, @player.x+14, @player.y+7, 1, 2, -15, 185, true,@projectilecolor1))
        end
        @player.jet_fire
        @firing_mode = true
      end
    else
      @a = 0
      @firing_mode = false
    end
    
    if $screen.button_down? Gosu::Button::KbLeft  and !@player.died then
      @player.strafe_left                      
      if $screen.button_down? Gosu::Button::KbDown and  !$screen.button_down? Gosu::Button::KbRight  then
        thrustersLeft
      elsif  $screen.button_down? Gosu::Button::KbUp
        thrustersLeftForward
      elsif  !$screen.button_down? Gosu::Button::KbDown
        thrustersLeftDown
      end
    end
    if $screen.button_down? Gosu::Button::KbRight or $screen.button_down? Gosu::Button::GpRight and !@player.died  then
      @player.strafe_right                    
      if $screen.button_down? Gosu::Button::KbDown and !$screen.button_down? Gosu::Button::KbLeft  then
        thrustersRight
      elsif $screen.button_down? Gosu::Button::KbUp
        thrustersRightForward
      elsif !$screen.button_down? Gosu::Button::KbDown
        thrustersRightDown
      end
    end
    if $screen.button_down? Gosu::Button::KbUp and !@player.died then
      @player.move_up                   
      if !$screen.button_down? Gosu::Button::KbRight  and !$screen.button_down? Gosu::Button::KbLeft  then
        thrustersForward
      end
    end
    if $screen.button_down? Gosu::Button::KbDown and !@player.died then
      @player.move_down                   
      if !$screen.button_down? Gosu::Button::KbLeft  and  !$screen.button_down? Gosu::Button::KbRight
        thrustersDown
      end
    end
    if !$screen.button_down? Gosu::Button::KbLeft  and !$screen.button_down? Gosu::Button::KbRight  and !$screen.button_down? Gosu::Button::KbDown  and !$screen.button_down? Gosu::Button::KbUp 
      thrustersIdle
    end
    def button_down(id)                 #helps with determining if the spacebar was held down for firing projectiles from the spacecraft above, also Esc key goes to Pause screen
      if id == Gosu::Button::KbLeftControl
        @firing_mode = false
      elsif id == Gosu::Button::KbEscape
        @pause = true
      elsif id == Gosu::Button::KbLeftShift
        if @guns == 2
        @guns = 0
        else
        @guns += 1
      end
      end
    end
  end
  
  def button_down(id)
  end
  #Updates all environment particles (explosions)
  def particleUpdate
    @particles.reject! { |particle| !particle.update}
  end
  
  #Commands at pauseScreen
  def pauseScreen
    def button_down(id)                 
        if id == Gosu::Button::KbReturn
          $game = Frontend.new
        elsif id == Gosu::Button::KbEscape
          @pause = false
        end
    end
  end
  
  def thrustersRight
     @b += 1
    if @b % 4 == 0
      1.times {@particles.push( Particle.new(ZOrder::Player,@player.x ,rand(0) + @player.y + @player.radius, @thrusters, (rand(100) - 50.0) / 1000.0, 0.75, rand(360), 0,  0.3, 0.05, 20))}
      1.times {@particles.push( Particle.new(ZOrder::Player,@player.x+12 ,rand(0) + @player.y + @player.radius, @thrusters, (rand(100) - 50.0) / 1000.0, 0.75, rand(360), 0,  0.15, 0.03, 20))}
      1.times {@particles.push( Particle.new(ZOrder::Player,@player.x-12 ,rand(0) + @player.y + @player.radius, @thrusters, (rand(100) - 50.0) / 1000.0, 0.75, rand(360), 0,  0.15, 0.03, 20))}
      @b = 0
    end
  end
  
  def thrustersRightDown
     @b += 1
    if @b % 4 == 0
      1.times {@particles.push( Particle.new(ZOrder::Player,@player.x ,rand(0) + @player.y + @player.radius, @thrusters, (rand(100) - 50.0) / 1000.0, 0.75, rand(360), 0,  0.3, 0.05, 20))}
      1.times {@particles.push( Particle.new(ZOrder::Player,@player.x+12 ,rand(0) + @player.y + @player.radius , @thrusters, (rand(100) - 50.0) / 1000.0, 0.75, rand(360), 0,  0.15, 0.03, 20))}
      1.times {@particles.push( Particle.new(ZOrder::Player,@player.x-12 ,rand(0) + @player.y + @player.radius , @thrusters, (rand(100) - 50.0) / 1000.0, 0.75, rand(360), 0,  0.15, 0.03, 20))}
      @b = 0
    end
  end
  
  def thrustersForward
      1.times {@particles.push( Particle.new(ZOrder::Player,@player.x ,rand(3) + @player.y + @player.radius, @thrusters, (rand(100) - 50.0) / 1000.0, 0.75, rand(360), 0,  0.3, 0.05, 70))}
      1.times {@particles.push( Particle.new(ZOrder::Player,@player.x+12 ,rand(3) + @player.y + @player.radius , @thrusters, (rand(100) - 50.0) / 1000.0, 0.75, rand(360), 0,  0.15, 0.03, 70))}
      1.times {@particles.push( Particle.new(ZOrder::Player,@player.x-12 ,rand(3) + @player.y + @player.radius, @thrusters, (rand(100) - 50.0) / 1000.0, 0.75, rand(360), 0,  0.15, 0.03, 70))}
  end
  
   def thrustersLeftForward
      1.times {@particles.push( Particle.new(ZOrder::Player,@player.x ,rand(3) + @player.y + @player.radius, @thrusters, (rand(100) - 50.0) / 1000.0, 0.75, rand(360), 0,  0.3, 0.05, 70))}
      1.times {@particles.push( Particle.new(ZOrder::Player,@player.x+12 ,rand(3) + @player.y + @player.radius, @thrusters, (rand(100) - 50.0) / 1000.0, 0.75, rand(360), 0,  0.15, 0.03, 70))}
      1.times {@particles.push( Particle.new(ZOrder::Player,@player.x-10 ,rand(3) + @player.y + @player.radius - 1, @thrusters, (rand(100) - 50.0) / 1000.0, 0.75, rand(360), 0,  0.15, 0.03, 70))}
  end
  
   def thrustersRightForward
      1.times {@particles.push( Particle.new(ZOrder::Player,@player.x ,rand(3) + @player.y + @player.radius, @thrusters, (rand(100) - 50.0) / 1000.0, 0.75, rand(360), 0,  0.3, 0.05, 70))}
      1.times {@particles.push( Particle.new(ZOrder::Player,@player.x+10 ,rand(3) + @player.y + @player.radius - 1, @thrusters, (rand(100) - 50.0) / 1000.0, 0.75, rand(360), 0,  0.15, 0.03, 70))}
      1.times {@particles.push( Particle.new(ZOrder::Player,@player.x-12 ,rand(3) + @player.y + @player.radius, @thrusters, (rand(100) - 50.0) / 1000.0, 0.75, rand(360), 0,  0.15, 0.03, 70))}
  end
  
  def thrustersDown
     @b += 1
    if @b % 4 == 0
      1.times {@particles.push( Particle.new(ZOrder::Player,@player.x ,rand(0) + @player.y + @player.radius, @thrusters, (rand(100) - 50.0) / 1000.0, 0.75, rand(360), 0,  0.3, 0.05, 20))}
      1.times {@particles.push( Particle.new(ZOrder::Player,@player.x+12 ,rand(0) + @player.y + @player.radius, @thrusters, (rand(100) - 50.0) / 1000.0, 0.75, rand(360), 0,  0.15, 0.03, 20))}
      1.times {@particles.push( Particle.new(ZOrder::Player,@player.x-12 ,rand(0) + @player.y + @player.radius, @thrusters, (rand(100) - 50.0) / 1000.0, 0.75, rand(360), 0,  0.15, 0.03, 20))}
      @b = 0
    end
  end
  
  def thrustersLeft
     @b += 1
    if @b % 4 == 0
      1.times {@particles.push( Particle.new(ZOrder::Player,@player.x ,rand(0) + @player.y + @player.radius, @thrusters, (rand(100) - 50.0) / 1000.0, 0.75, rand(360), 0,  0.3, 0.05, 20))}
      1.times {@particles.push( Particle.new(ZOrder::Player,@player.x+12 ,rand(0) + @player.y + @player.radius, @thrusters, (rand(100) - 50.0) / 1000.0, 0.75, rand(360), 0,  0.15, 0.03, 20))}
      1.times {@particles.push( Particle.new(ZOrder::Player,@player.x-12 ,rand(0) + @player.y + @player.radius, @thrusters, (rand(100) - 50.0) / 1000.0, 0.75, rand(360), 0,  0.15, 0.03, 20))}
      @b = 0
    end
  end
  
  def thrustersLeftDown
     @b += 1
    if @b % 4 == 0
      1.times {@particles.push( Particle.new(ZOrder::Player,@player.x ,rand(0) + @player.y + @player.radius, @thrusters, (rand(100) - 50.0) / 1000.0, 0.75, rand(360), 0,  0.3, 0.05, 20))}
      1.times {@particles.push( Particle.new(ZOrder::Player,@player.x+12 ,rand(0) + @player.y + @player.radius, @thrusters, (rand(100) - 50.0) / 1000.0, 0.75, rand(360), 0,  0.15, 0.03, 20))}
      1.times {@particles.push( Particle.new(ZOrder::Player,@player.x-12 ,rand(0) + @player.y + @player.radius, @thrusters, (rand(100) - 50.0) / 1000.0, 0.75, rand(360), 0,  0.15, 0.03, 20))}
      @b = 0
    end
  end
  
  def thrustersIdle
    @b += 1
    if @b % 4 == 0
      1.times {@particles.push( Particle.new(ZOrder::Player,@player.x ,rand(0) + @player.y + @player.radius, @thrusters, (rand(100) - 50.0) / 1000.0, 0.75, rand(360), 0,  0.3, 0.05, 20))}
      1.times {@particles.push( Particle.new(ZOrder::Player,@player.x+12 ,rand(0) + @player.y + @player.radius , @thrusters, (rand(100) - 50.0) / 1000.0, 0.75, rand(360), 0,  0.15, 0.03, 20))}
      1.times {@particles.push( Particle.new(ZOrder::Player,@player.x-12 ,rand(0) + @player.y + @player.radius, @thrusters, (rand(100) - 50.0) / 1000.0, 0.75, rand(360), 0,  0.15, 0.03, 20))}
      @b = 0
    end
  end
end 